﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;

namespace CoolERP_Web.User
{
    public partial class UserManage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AspNetPager1.PageSize = GridView1.PageSize;
                BindGridview();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbIds = new StringBuilder();
            foreach (GridViewRow container in GridView1.Rows)
            {
                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                if (cBox != null && cBox.Checked)
                    sbIds.Append(string.Format(",{0}", cBox.Value));
            }
            if (sbIds.Length > 0)
            {
                sbIds = sbIds.Remove(0, 1);
                using (UsersBLL pBLL = new UsersBLL())
                {
                    if (pBLL.Deletes("Id", sbIds.ToString()))
                        this.ShowMessage("删除成功");
                    else
                        this.ShowMessage("删除失败");
                }
                BindGridview();
            }
        }

        private void BindGridview()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(tbCode.Text))
                dc.Add("Code", tbCode.Text.Trim());
            if (!string.IsNullOrEmpty(tbName.Text))
                dc.Add("Name", tbName.Text.Trim());
            using (UsersBLL uBLL = new UsersBLL())
            {
                int RecordCount = 0;
                GridView1.DataSource = uBLL.GetUsers(dc, "Code asc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                GridView1.DataBind();
                this.AspNetPager1.RecordCount = RecordCount;
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindGridview();
        }
    }
}